package ca.cbc.msb.daemon;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.cbc.msb.util.FTPUtil;

public class YospaceExpireHandlerJob implements StatefulJob{
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	private static boolean IS_ENABLE_FTPDELETE_CRON;
	
//	private MediaPersistence mediaPersistence;
//	private MobileMediaService mobileMediaService;
//	private MediaService mediaService;
	
	public YospaceExpireHandlerJob() {
		IS_ENABLE_FTPDELETE_CRON = AppConfig.getProperty("enable.ftpdelete.cron").equalsIgnoreCase("true") ? true : false;
//		mediaPersistence = ApplicationContextUtil.instance.getApplicationContext().getBean(MediaPersistence.class);
//		mobileMediaService = new YospaceServiceImpl();
//		mediaService = new MediaService();
	}
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
//		int first = 0;
//		int max = 1000;
//		boolean hasMore = true;
//		while (hasMore) {
//			List<Media> expiredMedias = mediaPersistence.listExpiredMobileMedia(first, max);
//            hasMore = expiredMedias.size() == max;
//            int i=0;
//            for (Media m : expiredMedias) {
//            	if (m.getMobileId() != 0) {
//            		i++;
//            		//remove from the platform
//                    try {
//                    	mediaService.deleteYospaceId(m.getReleaseId(), m.getId());
//                    	logger.debug("YospaceId ({}) removing from thePlatform succesfully..", m.getReleaseId());
//                    } catch (ThePlatformObjectNotFoundException e) {
//                    	logger.warn("!This Release has already been deleted from thePlatform, no need to remove yospaceId...");
//                    }
//
//                    //remove from yospace
//                    mobileMediaService.deleteMedia(m.getMobileId());
//                    ProgressReport.instance.report(m.getTitle() + "-" + m.getReleaseId() + " expired  and removed from Yospace");
//                    logger.debug("Record ({}) removing from yospace succesfully..", m.getMobileId());
//
//                    //remove from db
//                    mediaPersistence.delete(m);
//                    logger.info("Media ({}) removing from thePlatform, yospace and db succesfully..", m.getId());
//                    }
//            }
//            logger.info("{} expired medias have been removed.", i);
//            first += max;
//		}
		if(IS_ENABLE_FTPDELETE_CRON) {
			FTPUtil.deleteOldFiles();
		}
	}
}
